/**
 * 付款界面
 * author:zhangjlt
 * date:2017年3月16日13:30:42
 */
;
define(function(require, exports, module) {
	var wechat = require("wechat");
	var template = require("template");
	var occupation = {
		data: {},
		/**
		 * bindEvent 页面事件
		 */
		bindEvent: function() {
			//返回
			$("#return_occupation").unbind().on("click", function() {
				window.location.href = "#/index/myProfile";
			});
		},
		/**
		 * 进行页面路由监听配置
		 * 
		 */
		bindRout: function() {
			//路由
			Path.map("#/index/:page_name").to(function() {
				console.log(this.params["page_name"]);
				var page_name = this.params["page_name"];
				//记录状态
				common.loadPage({
					pageUrl: "html/" + page_name + ".html",
					reload: true,
					from: "left"
				});
			});
			Path.listen();
		},
		/**
		 * 页面数据加载
		 */
		initData: function() {
			var _this = this;
			var param = {
				"openId": window.localStorage.getItem("openId"),
				"appId": window.localStorage.getItem("appId"),
			};
			common.loadData({
				url: "/wxmember/service/getAllJobs",
				param: param,
				successCallBack: _this.getAllJobsCallBack, //回调函数
				dataSource: "getAllJobs"
			});
			var str = window.localStorage.getItem("userInfoData");
			var userInfo = JSON.parse(str);
			var jobId = userInfo.jobId;
			$(".item_occupation").each(function(){
				if($(this).attr("jobid")==jobId){
					$(this).removeClass("nochecked");
					$(this).addClass("checked1");
				}
			});
			
			

		},
		saveOccupation: function(job,jobId) {
			var param = {
				"openId": window.localStorage.getItem("openId"),
				"appId": window.localStorage.getItem("appId"),
				"job": job,
				"jobId": jobId,
			}
			common.loadData({
				url: "/wxmember/service/changeUserInfo",
				param: param,
				successCallBack: function(res) {
					if(res.status == "0"){
					common.showTip({
						content: "修改成功"
					});
//					window.location.href = "#/index/myProfile";
					window.history.back(-1);
					}
				}, //回调函数
				dataSource: "getUserInfo",
				cache: false
			});
		},
		//callback function
		getAllJobsCallBack: function(json) {
			console.log("调用后台ajax返回值为：" + JSON.stringify(json));
			if(json.status == "0") {
				occupation.data.list = json.data;
				var html2 = template('occupationTemplate', occupation.data);
				$("#main_occupation").html(html2);
				console.log($(".click").length);
				$(document).off("click", '.item_occupation').on("click", '.item_occupation', function() {
					$(".checked1").removeClass("checked1").addClass("nochecked");
					$(this).removeClass("nochecked");
					$(this).addClass("checked1");
					var job = $(this).find("span").text();
					var jobId = $(this).attr("jobid");
					occupation.saveOccupation(job,jobId);
				});
			} else {
				common.showTip({
					content:json.message
				});
			}
			//iscroll滚动加入
				var iscroll = require("iscroll");
				common.initIscroll({
					container: "iscrollContent_occupation",
					showScrollBar: false,
					
				});

		},
		/**
		 * each page init mothed
		 */
		initialize: function() {
			//动态加载数据
			this.initData();
			//事件绑定
			this.bindEvent();
			//路由绑定
			this.bindRout();
		}
	};

	module.exports = occupation;
});